﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;

namespace CRM.Common
{
    /// <summary>
    /// Base64加密类
    /// </summary>
    public class Base64
    {
        /// <summary>
        /// 将字符串使用base64算法加密
        /// </summary>
        /// <param name="SourceString">待加密的字符串</param>
        /// <param name="Ens">System.Text.Encoding 对象，如创建中文编码集对象：
        /// System.Text.Encoding.GetEncoding("gb2312")</param>
        /// <returns>编码后的文本字符串</returns>
        public static string EncodingString(string SourceString, System.Text.Encoding Ens)
        {
            return Convert.ToBase64String(Ens.GetBytes(SourceString));
        }

        /// <summary>
        /// 使用缺省的代码页将字符串使用base64算法加密
        /// </summary>
        /// <param name="SourceString">待加密的字符串</param>
        /// <returns>加密后的文本字符串</returns>
        public static string EncodingString(string SourceString)
        {
            return EncodingString(SourceString, System.Text.Encoding.Default);
        }

        /// <summary>
        /// 从base64编码的字符串中还原字符串，支持中文
        /// </summary>
        /// <param name="Base64String">Base64加密后的字符串</param>
        /// <param name="Ens">System.Text.Encoding对象，如创建中文编码集对象：
        /// System.Text.Encoding.Default</param>
        /// <returns>还原后的文本字符串</returns>
        public static string DecodingString(string Base64String, System.Text.Encoding Ens)
        {
            return Ens.GetString((Convert.FromBase64String(Base64String)));
        }

        /// <summary>
        ///使用缺省的代码页从Base64编码的字符串中还原字符串，支持中文
        /// </summary>
        /// <param name="Base64String">Base64加密后的字符串</param>
        /// <returns>还原后的文本字符串</returns>
        public static string DecodingString(string Base64String)
        {
            return DecodingString(Base64String, System.Text.Encoding.Default);
        }
    }

    /// <summary>
    /// DES加密类
    /// </summary>
    public class DES
    {
        public static string GenerateKey()
        {
            // 创建一个DES 算法的实例。自动产生Key
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            // 返回自动创建的Key 用于加密
            return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
        }

        /// <summary>
        /// DES 加密 Key
        /// </summary>
        public static string DESKey
        {
            get
            {
                if (ConfigurationManager.AppSettings["desEncryKey"] != null)
                    return ConfigurationManager.AppSettings["desEncryKey"];

                return "agijkmpk";
            }
        }

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="sInputString">输入字符</param>
        /// <param name="sKey">Key</param>
        /// <returns>加密结果</returns>
        public static string EncryptString(string sInputString, string sKey)
        {
            byte[] data = Encoding.Default.GetBytes(sInputString);
            byte[] result;
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            result = desencrypt.TransformFinalBlock(data, 0, data.Length);

            string desString = "";
            for (int i = 0; i < result.Length; i++)
            {
                desString += result[i].ToString() + "-";
            }

            //return desString.TrimEnd('-');
            return BitConverter.ToString(result);
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="sInputString">输入字符</param>
        /// <param name="sKey">Key</param>
        /// <returns>解密结果</returns>
        public static string DecryptString(string sInputString, string sKey)
        {
            string[] sInput = sInputString.Split("-".ToCharArray());
            byte[] data = new byte[sInput.Length];
            byte[] result;
            for (int i = 0; i < sInput.Length; i++)
                data[i] = byte.Parse(sInput[i], System.Globalization.NumberStyles.HexNumber);

            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateDecryptor();
            result = desencrypt.TransformFinalBlock(data, 0, data.Length);
            return Encoding.Default.GetString(result);
        }
    }

    /// <summary>
    ///四重加密（采用两层base64+两层DES加密）密钥为当前的网址
    /// </summary>
    public class DoubleEncodeHelper
    {
        #region 四重加密（采用两层base64+两层DES加密）密钥为当前的网址

        /// <summary>
        /// 四重加密
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Encode(string inputString, string sKey)
        {
            //首先进行DES加密
            string content = DES.EncryptString(inputString, sKey);

            //然后进行base64编码
            content = Base64.EncodingString(content);

            //然后在进行DES加密
            content = DES.EncryptString(content, sKey);

            //最后再进行base64编码
            content = Base64.EncodingString(content);

            return content;
        }
        #endregion

        #region 四重解密（采用Base64解密+两层DES解密）密钥为登录网址
        /// <summary>
        /// 四重解密（采用Base64解密+两层DES解密）密钥为登录网址
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Uncode(string inputString, string sKey)
        {
            //首先进行base64解密
            inputString = Base64.DecodingString(inputString);

            //然后用DES解密
            inputString = DES.DecryptString(inputString, sKey);

            //然后再用base64解密
            inputString = Base64.DecodingString(inputString);

            //最后用DES解密
            inputString = DES.DecryptString(inputString, sKey);

            return inputString;
        }
        #endregion
    }

    #region  MD5加密类
    /// <summary>
    /// MD5加密类
    /// </summary>
    public class MD5
    {
        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="sInputString">输入文本</param>
        /// <returns></returns>
        public string HashString(string sInputString)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            string encoded = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(sInputString))).Replace("-", "");
            return encoded;
        }
    }
    #endregion
}

